package cn.whitecao.agent.core.handler;


import cn.whitecao.agent.core.global.GlobalVal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author : BashfulHuang
 **/
public class HandlerLoader {
    private final static Map<String,Class<?>> HANDLER_CACHE=new ConcurrentHashMap<>();
    private final static ClassLoader CLASS_LOADER= GlobalVal.externalJarClassLoader;
    public static PluginAopHandler load(String className){
        try {
            //后面把这个缓存起来,如果有就不需要通过loadClass去遍历外部jar读取并加载
            //这里保存类而不是保存实例化好的对象是因为要考虑到在类里面使用成员变量的问题
            Class<?> clazz = HANDLER_CACHE.get(className);
            if (clazz==null){
                clazz = CLASS_LOADER.loadClass(className);
                HANDLER_CACHE.put(className,clazz);
            }
            Object instance = clazz.newInstance();
            return (PluginAopHandler)instance;
        } catch (Exception e) {
            return null;
        }

    }
}
